import { makeAutoObservable } from "mobx";
import { LoginUser , LoginVerify , LoginOut } from '@/services/modules/Login'
import { getCookie, removeCookie, setCookie } from '@/utils/auth';

class Login{
    authorization: string = '';
    constructor(){
        makeAutoObservable(this);
    }

    Verify : { img : string , msg : string , uuid : '' } = { 
        img : '' , 
        msg : '' , 
        uuid : '' 
    };

    async getViewIfy(){
        let result = await LoginVerify();
        this.Verify = { 
            img : result.img ,  
            msg : result.msg ,
            uuid : result.uuid
        };
    }

    async loginEvent(
        username : string,
        password : string, 
        uuid : string,
        code : string, 
        key: string
    ){
        let result = await LoginUser( username , password , uuid , code , key);

        let authorization = this.authorization = `Bearer ${result.token}`;
        console.log(result);
        if( result.msg === '操作成功' ){
            setCookie(authorization);
            console.log(result);
            
            return authorization;
        }
        
    }

    async logout(){
        removeCookie();
        await LoginOut();
        this.authorization = '';
    }
}

export default new Login;